package view
{
	import component.LContainer;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import models.ModelLocator;
	
	public class AccordianMotion extends Sprite
	{
		private var model:ModelLocator = ModelLocator.getInstance();;
		private var montionSprite:LContainer;
		
		/*private var chooseSizeItem:NavigationItem;
		private var chooseFrame:NavigationItem;
		private var adjustingStructure:NavigationItem;*/
		
		private var friction:Number = 0.8;
		private var dataAry:Array = [[model.embedVo.chooseSize,new ItemDetails,new ProportionList],
									 [model.embedVo.chooseFrame,new ItemDetails,new BorderList],
									 [model.embedVo.adjustingStructure,new SubmitDetail],
									];
		private var itemAry:Array;
		private var motionAry:Array;
		public function AccordianMotion(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this.x = xpos;
			this.y = ypos;
			if(parent != null)
			{
				parent.addChild(this);
			}
			init();
		}
		private function init():void{
			motionAry = new Array;
			itemAry = new Array;
			
			var cls:Class = model.embedVo.NavigationBg
			var bitmap:Bitmap = new cls();
			this.addChild(bitmap);
			montionSprite = new LContainer(this,0,0);
			montionSprite.y = 25;
			montionSprite.width = 158;
			montionSprite.height = 407;
			var h:Number = 407 - dataAry.length*30;
			
			for(var i:int;i<dataAry.length;i++){
				var item:NavigationItem = new NavigationItem(dataAry[i][0],dataAry[i][1],
												montionSprite,i*30,(i)*30 + h,i,onClick);
				if(dataAry[i][1] is ItemDetails && dataAry[i].length >= 3){
					ItemDetails(dataAry[i][1]).setchild(dataAry[i][2]);
				}
				itemAry.push(item);
			}
			onClick("default");
		}
		public function onClick(event:*=null):void{
			var item:NavigationItem;
			if(event is MouseEvent){
				item = event.currentTarget as NavigationItem;
			}else if(event == null){
				item = itemAry[2];
			}else if(event == "default"){
				item = itemAry[0];
			}
			
			var adjacentItem:NavigationItem;
			for(var i:int;i<itemAry.length;i++){
				adjacentItem = itemAry[i]
				if(adjacentItem.tagergeID <= item.tagergeID){
					setItemY(adjacentItem,0)
				}else{
					setItemY(adjacentItem,1)
				}
			}
			run();
			CanvasEdit(this.parent).showSize(item.tagergeID);
			if(item.tagergeID == 1){
				item.show();
			}
		}
		public function showEdit():void{
			
		}
		private function setItemY(adjacentItem:NavigationItem,type:int):void{
			if(type == 0){
				adjacentItem.targetY = adjacentItem.baseTargetY;
			}else if(type == 1){
				adjacentItem.targetY = adjacentItem.downTargetY;
			}
			if(adjacentItem.targetY == adjacentItem.y){
				return;
			}
			adjacentItem.vy = (adjacentItem.targetY - adjacentItem.y) * 0.3;
			motionAry.push(adjacentItem);
		}
		 
		private function run():void{
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		private function stop():void{
			this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		private function onEnterFrame(event:Event):void{
			var item:NavigationItem;
			for(var i:int=0;i<this.motionAry.length;i++){
				item = motionAry[i];
				moveTaget(item);
				if(Math.abs(item.targetY - item.y) < 1){
					item.y = item.targetY;
					this.motionAry.splice(i,1);
				}
			}
			if(motionAry.length == 0){
				stop();
			}
			trace(motionAry.length)
		}
		private function moveTaget(item:NavigationItem):void{
			item.vy *= friction;
			item.y += item.vy;
		}
	}
}